package thorsten.wordlist.parser;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MultiFileOutputHandlerDW  extends DefaultHandler{
	private long fileCount = 0;
	private long elementCount = 0;
	private String outDir = "o:/output/";
	private long maxFileCount = 10000000;
	private long dirCount = 0;
	private long filesPerDir = 2500;
	private Writer outfile;
	private long maxFileSize = 10000000;
	private long curFileSize;

	public MultiFileOutputHandlerDW() {
		super();
		this.startNewFile();
	}
		
	public void startNewFile() {

		if (this.outfile != null) {
			try {
				this.outfile.close();
				this.outfile = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		

		if (this.fileCount >= this.maxFileCount){
			return;
		}


		String filename = String.format("%0" + 10 + "d", this.fileCount);
		
		if(this.fileCount % filesPerDir == 0){
			this.dirCount++;
			String dir =  String.format("%0" + 10 + "d/", this.dirCount);
			new File(this.outDir + dir).mkdir();
		}
		String dir =  String.format("%0" + 10 + "d/", this.dirCount);

		filename = this.outDir + dir + filename;
		
		try {
			this.outfile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"));
			this.fileCount++;
			this.curFileSize = 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	public void writeout(char[] ch, int start, int length) throws IOException {
	
		if(this.outfile != null){
			this.outfile.write(ch, start, length);
			this.curFileSize += length;
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
//		String s=new String(ch);
//		s = s.substring(start, start + length);
		
//		String s = new String(ch, start, length);
		
		// TODO: Begrenzung beachten!
		
		try {
			this.writeout(ch, start, length);
		} catch (IOException e) {
			e.printStackTrace();
//			System.out.println(s);
//			System.out.println("Nicht geschrieben: " + s);
		}
		
		super.characters(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
	
		this.elementCount++;
			
		if(this.elementCount % 10000 == 0)
			System.out.println(this.elementCount);
		
		if(this.curFileSize > this.maxFileSize)
			this.startNewFile();
		else
			try {
				this.outfile.write("\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		super.endElement(uri, localName, qName);
	}
	

	@Override
	public void endDocument() throws SAXException {
		try {
			this.outfile.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Dokument beendet");
		System.out.println(this.fileCount + " Elemente geschrieben");
		System.out.println(this.elementCount + " Elemente gesamt");
		super.endDocument();
	}

	
}
